---@class SelectSkillAction : AbstractLeaf
local SelectSkillAction = ClientFight.CreateClass("SelectSkillAction", ClientFight.AbstractLeaf, ClientFight.IAction)
---@return EStatus
function SelectSkillAction:update()
    if self.fighter.totalSign.signs[FighterSignEnum.TAG_1000] then
        return EStatus.FAILURE
    end
    local eStatus = self:doSelectSkill()
    if eStatus ~= EStatus.SUCCESS then
        return eStatus
    end
    local skill = self.fighter.prepareSkill
    if skill == nil then
        return eStatus
    end
    skill.ruleSelectorByBuff = nil
    local ruleSelector = self.fighter:randomSkillTargetRuleSelector()
    if ruleSelector ~= nil then
        --如果有强行修改这次技能 规则的 buf效果, 那么设定给技能, 在技能使用成功前,清除该缓存
        skill.ruleSelectorByBuff = ruleSelector
    end
    return eStatus
end
---@return EStatus
function SelectSkillAction:doSelectSkill()
    SkillRoleMgr.savePathFile("[" .. self.fight.frame .. "][技能选择]" .. self. fighter:print() .. "进行技能选择");
    --不能使用技能
    if table.getn(self.fighter.skills) == 0 or FighterStateEnum.is(self.fighter, FighterStateEnum.CAN_NOT_USE_SKILL) then
        SkillRoleMgr.savePathFile("[" .. self.fight.frame .. "][技能选择]" .. self.fighter:print() .. "因为不能使用技能而选择普通攻击");
        return self:selectNormalAttack();
    end
    for k, skill in pairs(self.fighter.skills) do
        skill.ruleSelectorByBuff = nil
        local skip = false
        if self.fighter.skipSkill[skill.skillBean.f_SkillID] then
            skip = true
        end
        if skill.skillBean.f_TriggerType == SkillActionTypeEnum.TRIGGER then
            skip = true
        end
        if not SkillLimitType:action(self.fighter, skill.skillBean) then
            skip = true
        end
        if self.fighter.energy < self.fighter:calUseSp(skill) then
            skip = true
        end
        if ClientFight.SkillClientManager.isCooldowning(self.fighter, skill.skillId) then
            skip = true
        end
            if self.fighter:calUseSp(skill) > 0 and self.fighter.teamIndex == 1 and not FightManager.isAutoSkill  then
                --大招
                skip = true
            end
        if not skip then
            SkillRoleMgr.savePathFile("[" .. self.fight.frame .. "][技能选择]" .. self.fighter:print() .. "进行选择技能：" .. skill.skillBean.f_SkillID)

            self.fighter:setPrepareSkill(skill)
            return EStatus.SUCCESS;
        end
    end
    return self:selectNormalAttack()
end
function SelectSkillAction:selectNormalAttack()
    if FighterStateEnum.is(self.fighter, FighterStateEnum.CAN_NOT_NORMAL_ATTACK) then
        return EStatus.FAILURE
    end
    if ClientFight.SkillClientManager.isCooldowning(self.fighter, self.fighter.normalAttack.skillBean.f_SkillID) then
        return EStatus.FAILURE
    end
    local fighters = FighterUtils.getOthersBy(self.fighter, true, nil)
    for k, f in pairs(fighters) do
        if DistanceUtil.isInRange(self.fighter.position, f.position, 0.1) then
            local bool = math.random(0, 1) == 1--self.fighter.fight:random():nextBoolean()
            local boo2 = math.random(0, 1) == 1--self.fighter.fight:random():nextBoolean()
            local x = self.fighter.position.x + 0.2 * (bool and 1 or -1)
            local y = self.fighter.position.y + 0.2 * (bool and 1 or -1)
            FighterManager.changePosition(self.fight, self.fighter, 0, x, y, 0, true, true);
            break;
        end
    end
    self.fighter:setPrepareSkill(self.fighter.normalAttack)
    return EStatus.SUCCESS
end